package fr.mywiki.model.display;

import java.io.Serializable;
import java.util.Iterator;
import java.util.Map;

/**
 * Represents a link to an entry.
 * 
 * @author tlombard
 */
public class LinkToEntry implements Serializable {
	private static final long serialVersionUID = 1L;
	
	private String title;
	private String id;

	/** Forbidden constructor */
	private LinkToEntry() {}
	
	public LinkToEntry(String id,String title) {
		this.title = title;
		this.id = id;
	}
	
	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}
	
	/**
	 * Builds and returns a link to the entry. The link has the following form :
	 * <code>actionname.do?method=amethod&param=aparam</code>. For each element in the map, the
	 * key is mapped to the value via the sign '=', for example 'key=value'.
	 * 
	 * @param actionName a <code>String</code> object which is the name of an action as given in
	 *            struts-config.xml.
	 * @param params a <code>Map</code> object whose keys and values are <code>String</code>
	 *            objects.
	 * @return a String object representing a relative url.
	 * 
	 * TODO Move somewhere else ? LinkToEntry for example or something inside web.struts.
	 */
	public static String getLinkToEntry(String actionName, Map params) {
		StringBuffer url = new StringBuffer();

		url.append(actionName + ".do?");

		Iterator itParams = params.keySet().iterator();
		if (itParams.hasNext()) {
			String key = (String) itParams.next();
			url.append(key + "=" + params.get(key));
		}
		while (itParams.hasNext()) {
			String key = (String) itParams.next();
			url.append("&"+key + "=" + params.get(key));
		}

		return url.toString();
	}
}
